import { idCardValidate, licenseValidate } from '@vimi/utils-tool'
// 验证用户名

export const checkPhoneNumber = value => {
  return /(^1[3456789][0-9]{9}$)|(^0[0-9]{2,3}-?[0-9]{7,8})/.test(value)
}

export const checkMobile = value => {
  return /(^1[3456789][0-9]{9}$)/.test(value)
}

export const checkPassWord = value => {
  return /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=,./?<>]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=,./?<>]+$)(?![0-9\W_!@#$%^&*`~()-+=,./?<>]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=,./?<>]{8,30}$/.test(
    value
  )
}

export const passWordValidator = (rule, value, callback) => {
  if (!checkPassWord(value)) {
    callback(
      new Error('密码必须包含数字、大写字母、小写字母、特殊字字符至少3种,长度必须大于或等于8位')
    )
  } else {
    callback()
  }
}

export const nameValidator = (rule, value, callback) => {
  if (!/^[0-9a-zA-Z-_]*$/g.test(value)) {
    callback(new Error('只能由英文、数字、下划线组成'))
  } else {
    callback()
  }
}

export const emailValidator = (rule, value, callback) => {
  if (!/^\w+@[a-z0-9]+\.[a-z]{2,4}$/.test(value)) {
    callback(new Error('邮箱格式错误'))
  } else {
    callback()
  }
}

// 验证手机号码或座机是否正确，注意此处不验证空值
export const phoneValidator = (rule, value, callback) => {
  if (value === '') {
    callback()
  } else if (!checkPhoneNumber(value)) {
    callback(new Error('手机号码有误，请重新输入'))
  } else {
    callback()
  }
}

// 验证手机号码是否正确，注意此处不验证空值
export const mobileValidator = (rule, value, callback) => {
  if (value === '') {
    callback()
  } else if (!/(^1[3456789][0-9]{9}$)/.test(value)) {
    callback(new Error('手机号码有误，请重新输入'))
  } else {
    callback()
  }
}

// 验证身份证号
export const idCardValidator = (rule, value, callback) => {
  if (value === '') {
    callback()
  } else if (!idCardValidate(value)) {
    callback(new Error('错误的身份证号，请重新输入'))
  } else {
    callback()
  }
}

// 统一信用代码校验
export const registerNoValidator = (rule, value, callback) => {
  if (value === '') {
    callback()
  } else if (!licenseValidate(value)) {
    callback(new Error('统一信用代码有误，请重新输入'))
  } else {
    callback()
  }
}

// 是否以IP地址开头
export const isIP = value =>
  value &&
  /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/.test(value)
